# -*- coding: utf8 -*-  
'''
Created on 
网站下载demo
专用于某个网站下载
需要安装以下插件
pip install pyquery
pip install requests
pip install pyinstaller
编译方式:pyinstaller -F vvicdown.py --noconsole
@author: eruca
'''

from pyquery import PyQuery as pq


import os
import tkinter as tk
import requests 
window = tk.Tk()
window.title('eruca制作vvic.com专用下载工具v3')
window.geometry('500x500')
e = tk.Entry(window,show=None)
e.pack()

def zhanshiyedown(var,dss,html):

    thumbnail=pq(html('.thumbnail').html())
    thumbnailength=thumbnail("a").length;
    for idx in range(thumbnailength):
        try:
            aurl=pq(thumbnail("a").eq(idx).html()).attr('big');
            url = 'http:'+aurl
            print(url)
            print(dss+var+"_"+str(idx)+".jpg")
            r = requests.get(url) 
            with open(dss+var+"_"+str(idx)+".jpg", "wb") as code:
                code.write(r.content)
                t.insert('insert','已经下载保存到目录:'+dss+var+str(idx)+".jpg")
                t.insert('insert','\n图片URL地址\n')
                t.insert('insert',aurl)
                t.insert('insert','\n')
        except:
            print("下载失败")
        
def xiangqingyedown(var,dss,html):
    script=html("script");
    ilenx=script.length
    print(ilenx)
    for idx in range(ilenx):
        try:
            if pq(script.eq(idx).html()).attr("id")=='descTemplate':
                print(script.eq(idx).html())
        except TypeError:
            print('没有ID')
        else:
            xqy=pq(script.eq(idx).html())
            xqylen=xqy("img").length
            print(xqylen)
            for xqyidx in range(xqylen) :
                url=xqy("img").eq(xqyidx).attr("src")
                print(url)
                print(dss+var+"_"+str(xqyidx)+".jpg")
                r = requests.get(url) 
                with open(dss+var+"_xq_"+str(xqyidx)+".jpg", "wb") as code:
                    code.write(r.content)
            print('完毕')
            break;
def insert_point():
    var = e.get()
    
    dss='d://dongsisi/'+var+'/'
    print(dss)
    if os.path.exists(dss)!=True:
        os.makedirs(dss) 
    html = pq(url='https://www.vvic.com/item/'+var)
    zhanshiyedown(var, dss, html);
    xiangqingyedown(var, dss, html);
    
    
lb=tk.Label(window,text="eruca制作,V3专用于vvic.com网站图片下载用法如下");
lb.pack()
lb2=tk.Label(window,text="例如网址:https://www.vvic.com/item/8192488");
lb2.pack()

lb3=tk.Label(window,text="请在上方空白处填写8192488,再点击下方分析网址,固定保存在D盘dongsisi目录下");
lb3.pack()    

b1 = tk.Button(window,text='分析网址',width=15,
            height=2,command=insert_point)
b1.pack()

t = tk.Text(window,height=15)
t.pack()

window.mainloop()
#8192488 